Kenneth Tay
Sep 26, 2019
http://web.stanford.edu/~kjytay/courses/stats32-aut2019/
## [1] 7
## [1] 2.718282
## [1] 1
## [1] 3.141593
## [1] Inf
TRUE
or FALSE
)NA
sc()
function, or using the :
shortcut## [1] "a" "b" "c"
c()
function, or using the :
shortcut## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
## [18] 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
## [35] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
## [52] 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
## [69] 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
## [86] 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
## [18] 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68
## [35] 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
## [18] 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68
## [35] 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
How can we get the odd numbers from 1 to 100 from even
?
How can we get the odd numbers from 1 to 100 from even
?
## [1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
## [24] 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91
## [47] 93 95 97 99
We can use the c()
function to concatenate vectors.
## [1] 1 2 3 4 5 3 1 2 3 4 5
To extract a subset of elements by their indices, put a vector of indices in square brackets
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
## [18] 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68
## [35] 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
## [1] 2
To extract a subset of elements by their indices, put a vector of indices in square brackets
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
## [18] 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68
## [35] 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
## [1] 6 8 10 12 14
To extract a subset of elements by their indices, put a vector of indices in square brackets
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
## [18] 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68
## [35] 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
## [1] 6 10
Note: even[3,5]
will not work!
To extract all except a few indices, put a negative sign before the vector of indices
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
## [18] 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68
## [35] 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
## [1] 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
## [18] 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72
## [35] 74 76 78 80 82 84 86 88 90 92 94 96 98 100
Use the length
function to figure out how many elements there are in a vector
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34
## [18] 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68
## [35] 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
## [1] 50
What happens if we try to extract an invalid index?
## numeric(0)
No error thrown!!
What happens if we try to extract an invalid index?
## numeric(0)
No error thrown!!
What happens if we try to extract an invalid index?
## numeric(0)
No error thrown!!
## [1] NA
No error thrown!!
If we try to assign a vector with different types, type coercion happens.
## [1] "1" "2" "a"
Two-dimensional analogs of vectors
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
Indexing: put the rows you want before the comma, columns you want after the comma
## [1] 4
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
What does A[c(1,3), c(2,4)]
return?
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
What does A[c(1,3), c(2,4)]
return?
## [,1] [,2]
## [1,] 4 10
## [2,] 6 12
list()
functionUse [[
or $
notation to refer to a specific key-value pair
## [1] "Honda"
## [1] "Fit" "CR-V" "Odyssey"
## votes_dem votes_gop
## 1 486351 91189
## 2 318 211
## 3 5904 10239
## # A tibble: 3 x 2
## votes_dem votes_gop
## <dbl> <dbl>
## 1 486351 91189
## 2 318 211
## 3 5904 10239
## 'data.frame': 3 obs. of 2 variables:
## $ votes_dem: num 486351 318 5904
## $ votes_gop: num 91189 211 10239
fueleconomy
: Package information on CRANhttps://cran.r-project.org/web/packages/fueleconomy/index.html
Optional material